package com.catbad.services.impl;

import com.catbad.Exception.PlayMsgException;
import com.catbad.domain.Film;
import com.catbad.domain.Play;
import com.catbad.domain.Room;
import com.catbad.mapper.FilmMapper;
import com.catbad.mapper.PlayMapper;
import com.catbad.mapper.RoomMapper;
import com.catbad.services.PlayServices;
import com.catbad.vo.PlayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PlayServicesImpl implements PlayServices {
    @Autowired
    PlayMapper playMapper;
    @Autowired
    FilmMapper filmMapper;
    @Autowired
    RoomMapper roomMapper;
    @Override
    public List<Map<String,Object>>getPlayList(Long roomId, Long filmId) {
        Map<String,Object> params = new HashMap<>();
        params.put("roomId",roomId);
        params.put("filmId",filmId);
        return playMapper.getPlayList(params);
    }
    @Override
    public List<Map<String,Object>> getPlayAndSeat(Long pid){
        return null;
    }

    @Override
    public Play getPlayById(Long pid) {
        return playMapper.selectByPrimaryKey(pid);
    }

    @Override
    public PlayVO getPlayForPage(Long cid, String valTime) {
        PlayVO playVO = new PlayVO();          //放回容器
        Map<String,Object> params = new HashMap<>();    //参数
        //新建排片需要的数据
        List<Film> filmList = filmMapper.selectAll();       //获得下拉列表电影表
        List<Map<String,Object>> roomList = roomMapper.selectAllMapByCid(cid);   //所有影厅的列表
        //编辑排片的数据
        List<Map<String,Object>> editPlayList = new ArrayList<>();   //编辑部分的所有内容容器
        params.put("time",valTime);
        params.put("cid",cid);
        List<Film> editFilmList = filmMapper.getPlayFilm(params);  //获得该影院，该天，排过的影片
        for(Film film :editFilmList){
            //编辑行,每一行的数据，影片，影厅集合（当天场次集合）
            params.put("filmId",film.getFilmId());
            Map<String,Object> editPlay = new HashMap<>();
            List<Map<String,Object>> editRoomList = roomMapper.selectAllMapByCid(cid);
            for(Map<String,Object> editRoom:editRoomList){
                params.put("roomId",editRoom.get("room_id"));
                List<Map<String,Object>> playList = playMapper.getPlayForEditRoom(params); //获得该影厅，该电影，该天
                if(playList!=null) {
                    editRoom.put("playList", playList);
                }
            }
            editPlay.put("film",film);
            editPlay.put("editRoomList",editRoomList);
            editPlayList.add(editPlay);
        }
        //将页面数据存入
        playVO.setEditPlayList(editPlayList);
        playVO.setFilmList(filmList);
        playVO.setRoomList(roomList);
        return playVO;
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public int savePlay(Long filmId, String[] msg, String time,Long cid) throws Exception {
        List<Map<String,Object>> paramsList = parsePlayMsg(filmId,msg,time,cid);
        if(paramsList!=null) {
            for (Map<String, Object> params : paramsList) {
                playMapper.savePlay(params);
            }
        }
        return 1;
    }

    @Override
    public int deletePlayForTime(String time, Long cid, Long filmId) {
        Map<String,Object> params = new HashMap<>();
        params.put("startime",time);
        params.put("cid",cid);
        params.put("filmId",filmId);
        return playMapper.deletePlayForTime(params);
    }

    private List<Map<String,Object>> parsePlayMsg(Long filmId, String[] msg, String time,Long cid)throws Exception{
        List<Map<String,Object>> paramsList = new ArrayList<>();
        List<Map<String,Object>> roomList = roomMapper.selectAllMapByCid(cid);   //所有影厅的列表
        //将字符串去掉\r\n
        for(int i =0;i< msg.length;i++){
            msg[i] = msg[i].replaceAll("\r\n","");
        }
        //一次处理一个影厅room的排片信息
        for(int i=0;i<msg.length;i++){
            Long roomId = (Long) roomList.get(i).get("room_id");
            String[] roomMsg = msg[i].split(" ");//拆分每个影厅的text ，内部包含多个场次的信息
            if(roomMsg[0].equals("")){
                continue;
            }
            if(roomMsg.length<=1){
                throw new PlayMsgException("格式错了");
            }
            for (int j = 0; j < roomMsg.length; ) {
                Map<String, Object> params = new HashMap<>();
                String startime = time + " " + roomMsg[j];
                String price = roomMsg[j + 1];
                params.put("roomId", roomId);
                params.put("filmId", filmId);
                params.put("startime", startime);
                params.put("price", price);
                paramsList.add(params);
                j += 2;
            }
        }
        return paramsList;
    }
}
